package com.example.springbootjson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.springbootjson.vo.School;
import com.example.springbootjson.vo.User;
import com.example.springbootjson.vo.UserVo;
import com.example.springbootjson.vo.UserVo2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @GetMapping("/user")
    public UserVo getUser() {
        UserVo userVo = new UserVo();
        userVo.setUserName("小明");
//        userVo.setAge(18);
//        userVo.setAddr("beijing");
//        School school = new School();
//        school.setName("beida");
//        userVo.setSchool(school);
        userVo.setSchoolId("33");
        return userVo;
    }

    @GetMapping("/user2")
    public UserVo2 getUser2() {
        UserVo2 userVo = new UserVo2();
        userVo.setName("小明");
        userVo.setAge(18);
        userVo.setAddr("beijing");
        School school = new School();
        school.setName("beida");
        userVo.setSchool(school);
        return userVo;
    }

    @PostMapping("/save")
    public UserVo save(@RequestBody UserVo userVo) {
        return userVo;
    }

    @GetMapping("/user3")
    public User user3() {
        User user = new User();
        user.setBirthday(LocalDateTime.now());
        return user;
    }

    @GetMapping("/user4")
    public JSONArray user4() {
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setAge(18);
        user.setName("张三");
        user.setBirthday(LocalDateTime.now());
        users.add(user);
        users.add(user);
        users.add(user);
        return JSONArray.parseArray(JSON.toJSONString(users));
    }

    @GetMapping("/user5")
    public List<User> user5() {
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setAge(18);
        user.setName("张三");
        user.setBirthday(LocalDateTime.now());
        users.add(user);
        users.add(user);
        users.add(user);
        return users;
    }
}
